﻿using Pandora;
using Spine.Unity;
using UnityEditor;
using UnityEngine;

namespace PandoraEditor
{
    public class SpineMaker
    {
        private static readonly string actionName = "ExecuteWrapSpine";
        public static readonly string input_path = "Assets/Art/spine/";
        private static readonly string output_path = "Assets/data/spine/";

        public static void MakeSpinePrefab()
        {
            var list = PdrFileUtil.GetDirsName(input_path);
            foreach (var name in list)
            {
                MakeSpineSinglePrefab(name);
            }

            Debug.Log($"{actionName}运行完毕");
            AssetDatabase.Refresh();
        }

        public static void MakeSpineSinglePrefab(string name)
        {
            var assetFile = PdrFileUtil.GetFilesName($"{input_path}{name}", "asset").FindAll(x => x.Contains("SkeletonData"))[0];
            var go = new GameObject(name);
            go.AddComponent<RectTransform>();
            go.AddComponent<CanvasRenderer>();
            var spine = go.AddComponent<SkeletonGraphic>();
            spine.skeletonDataAsset = AssetDatabase.LoadAssetAtPath<SkeletonDataAsset>(assetFile);
            PrefabUtility.SaveAsPrefabAsset(go, $"{output_path}{name}.prefab");
            Object.DestroyImmediate(go);
            Debug.Log($"更新spine:{name}");
        }
    }
}